package main;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by PuFan on 2019/2/27 23:10 /Recruit
 */
public class ExcelListener extends AnalysisEventListener {
    private static Map<String, List<Job>> maps = new HashMap<>();

    @Override
    public void invoke(Object object, AnalysisContext context) {
        //System.out.println("当前行：" + context.getCurrentRowNum());
        Info info = (Info) object;
        String companyName = info.getId1();
        if (companyName != null) {
            List<Job> infoList = maps.get(companyName);
            if (infoList == null) {
                infoList = new ArrayList<>();
            }
            infoList.add(setInfo(info));
            maps.put(companyName, infoList);
        }
    }

    public void doAfterAllAnalysed(AnalysisContext context) {
        final List<String> list = new ArrayList<>();
        maps.forEach((k, v) -> v.forEach(e -> {
            if (JobFilter.filter(e)) {
                list.add("\n" + k + e);
            }
        }));
        System.out.println(list);
    }

    private Job setInfo(Info info) {
        Job job = new Job();
        job.setId(info.getId0());
        job.setCompanyName(info.getId1());
        job.setJobName(info.getId2());
        job.setJobNum(info.getId3());
        job.setAcquire(info.getId4());
        return job;
    }
}
